package org.volicy.scheduled.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.volicy.scheduled.param.ScheduledParam;
import org.volicy.scheduled.resp.ScheduledResp;
import org.volicy.scheduled.sch.ScheduleHandler;

/**
 * @Author: volicy.xu
 * @Date: 2021/9/21
 */
@RestController
@RequestMapping("/scheduled")
public class ScheduledController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledController.class);
    @Autowired
    private ScheduleHandler scheduleHandler;

    /**
     *
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("index.html");
    }

    /**
     *
     * @return
     */
    @RequestMapping("/list")
    public ScheduledResp list(@RequestBody ScheduledParam scheduledParam){
        return ScheduledResp.successInstance(scheduleHandler.findByJobName(scheduledParam));
    }
    /**
     *
     * @param scheduledParam
     * @return
     */
    @RequestMapping("/pause")
    public ScheduledResp pause(@RequestBody ScheduledParam scheduledParam){
        scheduleHandler.cancel(scheduledParam.getJobExpress(), scheduledParam.getJobClassName(), scheduledParam.getId(),Boolean.TRUE);
        return ScheduledResp.successInstance();
    }
    /**
     *
     * @param scheduledParam
     * @return
     */
    @RequestMapping("/resume")
    public ScheduledResp resume(@RequestBody ScheduledParam scheduledParam){
        return exec("resume",scheduledParam);
    }
    /**
     *
     * @param scheduledParam
     * @return
     */
    @RequestMapping("/remove")
    public ScheduledResp remove(@RequestBody ScheduledParam scheduledParam){
        return exec("remove",scheduledParam);
    }

    @RequestMapping("/trigger")
    public ScheduledResp trigger(@RequestBody ScheduledParam scheduledParam){
        return exec("trigger",scheduledParam);
    }
    /**
     *
     * @param type
     * @param scheduledParam
     * @return
     */
    private ScheduledResp exec(String type,ScheduledParam scheduledParam){
        try{
            switch (type){
                case "resume":
                    scheduleHandler.resume(
                            scheduledParam.getJobClassName(),
                            scheduledParam.getJobMethodName(),
                            scheduledParam.getJobExpress(),
                            scheduledParam.getId());
                    break;
                case "remove":
                    scheduleHandler.remove(scheduledParam.getJobExpress(),scheduledParam.getJobClassName(), scheduledParam.getId());
                    break;
                case "trigger":
                    scheduleHandler.trigger(scheduledParam.getJobClassName(),scheduledParam.getJobMethodName());
                    break;
            }
            return ScheduledResp.successInstance();
        }catch (Exception ex){
            LOGGER.error("exec error",ex);
            return ScheduledResp.failInstance();
        }
    }
}
